package 
{ 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    
    import mx.containers.TitleWindow; 
    import mx.controls.Button; 
    import mx.core.Application; 
    import mx.effects.Resize; 
    import mx.events.EffectEvent; 
    import mx.events.FlexEvent; 
    import mx.events.ResizeEvent;
    import mx.controls.CheckBox; 
    

        
    public class MinMaxWindow
    extends TitleWindow 
    { 

        private var _showWindowButtons:Boolean = true; 
        
        private var windowOldHeight:Number = 0; 
        
        private var resizeEffect:Resize; 

  		[Embed(source="images/min.png")] 
        public var WINDOW_MIN_BUTTON_1:Class; 
        [Embed(source="images/min.png")] 
        public var WINDOW_MIN_BUTTON_2:Class; 
        [Embed(source="images/max.png")] 
        public var WINDOW_MAX_BUTTON_1:Class; 
        [Embed(source="images/max.png")] 
        public var WINDOW_MAX_BUTTON_2:Class; 
        [Embed(source="images/close.png")] 
        public var WINDOW_CLOSE_BUTTON_1:Class; 
        [Embed(source="images/close.png")] 
        public var WINDOW_CLOSE_BUTTON_2:Class; 
                
            
        public function MinMaxWindow() 
        { 
            super(); 

            this.addEventListener(FlexEvent.CREATION_COMPLETE, addButtons); 
            
//            this.setStyle("headerColors", [0x4a6c8d,0x56789b]); 
//            this.setStyle("borderAlpha","0.6"); 
            
//            this.setStyle("borderThicknessTop","1"); 
//            this.setStyle("borderThicknessBottom","1"); 
//            this.setStyle("borderThicknessRight","1"); 
//            this.setStyle("borderThicknessLeft","1"); 
//    
//            this.setStyle("dropShadowEnabled","false"); 
//            this.setStyle("headerHeight","15"); 
            
            this.verticalScrollPolicy = "off"; 
            this.percentWidth = 100; 
            
            resizeEffect = new Resize(this); 
            resizeEffect.duration = 500; 
            
            resizeEffect.addEventListener(EffectEvent.EFFECT_START, resizeEffectStart); 
            resizeEffect.addEventListener(EffectEvent.EFFECT_END, resizeEffectEnd); 
        } 
                
        public function set showWindowButtons(show:Boolean):void{ 
            _showWindowButtons = show; 
            if(titleBar != null){ 
                 addButtons(); 
            } 
        } 
                
        public function get showWindowButtons():Boolean{ 
            return _showWindowButtons; 
        } 
                
        public var windowMinButton:Button; 
        public var windowMaxButton:Button; 
        public var windowCloseButton:Button; 
                
                
        private function addButtons(event:FlexEvent = null):void{ 
            if(_showWindowButtons){ 
                if(windowMinButton == null){ 
                        windowMinButton = new Button(); 
                		windowMinButton.width=13; 
                        windowMinButton.height=13;
						windowMinButton.focusEnabled=false; 
                        windowMinButton.setStyle("upSkin",   WINDOW_MIN_BUTTON_1); 
                        windowMinButton.setStyle("overSkin", WINDOW_MIN_BUTTON_2); 
                        windowMinButton.setStyle("downSkin", WINDOW_MIN_BUTTON_2); 
                
//						windowMinButton.addEventListener(MouseEvent.CLICK, minWindow); 
                        titleBar.addChild(windowMinButton); 
                } 
                if(windowMaxButton == null){ 
                        windowMaxButton = new Button(); 
                        windowMaxButton.width=13; 
                        windowMaxButton.height=13; 
						windowMaxButton.focusEnabled=false; 
                        windowMaxButton.setStyle("upSkin", WINDOW_MAX_BUTTON_1); 
                        windowMaxButton.setStyle("overSkin",WINDOW_MAX_BUTTON_2); 
                        windowMaxButton.setStyle("downSkin",WINDOW_MAX_BUTTON_2); 
                
//						windowMaxButton.addEventListener(MouseEvent.CLICK, maxWindow); 
                        titleBar.addChild(windowMaxButton); 
                } 
                if(windowCloseButton == null){ 
                        windowCloseButton = new Button(); 
                        windowCloseButton.width=13; 
                        windowCloseButton.height=13; 
						windowCloseButton.focusEnabled=false; 
                        windowCloseButton.setStyle("upSkin", WINDOW_CLOSE_BUTTON_1); 
                        windowCloseButton.setStyle("overSkin",WINDOW_CLOSE_BUTTON_2); 
                        windowCloseButton.setStyle("downSkin",WINDOW_CLOSE_BUTTON_2); 
                
//						windowMaxButton.addEventListener(MouseEvent.CLICK, maxWindow); 
                        titleBar.addChild(windowCloseButton); 
                } 

                layoutWindowButtons(); 
            } else { 
                if(windowMinButton != null){ 
                    titleBar.removeChild (windowMinButton); 
                    windowMinButton = null; 
                    titleBar.removeChild (windowMaxButton); 
                    windowMaxButton = null; 
                    titleBar.removeChild (windowCloseButton); 
                    windowCloseButton = null; 
                } 
            } 
        } 
                
        private function layoutWindowButtons():void{ 
            if(windowMinButton != null){ 
                windowMinButton.move(titleBar.width - 45 , (titleBar.height - 10) / 2); 
            } 
            if(windowMaxButton != null){ 
                windowMaxButton.move(titleBar.width - 33, (titleBar.height - 10) / 2); 
            } 
            if(windowCloseButton != null){ 
                windowCloseButton.move(titleBar.width - 21, (titleBar.height - 10) / 2); 
            }
        } 
                
        private function minWindow(event:MouseEvent):void{ 
            if(this.height != 15){ 
                this.windowOldHeight = this.height; 
                resizeEffect.heightTo = 15; 
                resizeEffect.end(); 
                resizeEffect.play(); 
            } 
        } 
                
        private function maxWindow(event:MouseEvent):void { 
            if(this.windowOldHeight == 0) 
               this.windowOldHeight = this.height; 

            if(this.height == 15){ 
               resizeEffect.heightTo = this.windowOldHeight; 
               resizeEffect.end(); 
               resizeEffect.play(); 
            } 
        } 
                
        private function resizeEffectStart(event:EffectEvent):void{ 
            windowMinButton.mouseEnabled = false; 
            windowMaxButton.mouseEnabled = false; 
            windowCloseButton.mouseEnabled = false; 
        } 
                
                
        private function resizeEffectEnd(event:EffectEvent):void{ 
            windowMinButton.mouseEnabled   = true; 
            windowMaxButton.mouseEnabled   = true; 
            windowCloseButton.mouseEnabled = true; 
        } 
                
        override protected function layoutChrome(unscaledWidth:Number, unscaledHeight:Number):void{ 
            super.layoutChrome(unscaledWidth,unscaledHeight); 
            layoutWindowButtons(); 
        } 
    } 
} 